package easytasks.ui;

import java.io.Serializable;

import easytasks.data.DataManager;
import easytasks.data.DataSetup;
import easytasks.data.TaskCategory;
import easytasks.main.R;
import android.app.Activity;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class AppMenu 
{
	private Activity activity;
	private int menuItemToDisable;
	private Menu menu = null;
	
	private DataSetup dataInit;
	private DataManager dataManager;
	
	public AppMenu(Activity activity)
	{
		if (activity == null) throw new IllegalArgumentException("'activity' must be not null");
		this.activity = activity;
		
		dataInit = new DataSetup(activity);
        dataManager = new DataManager(dataInit, PreferenceManager.getDefaultSharedPreferences(activity));
	}
	
	public AppMenu(Activity actity, int menuItemToDisable)
	{
		this(actity);
		this.menuItemToDisable = menuItemToDisable;
	}
	
	public boolean onCreateOptionsMenu(Menu menu)
	{
		if (menu == null) throw new IllegalArgumentException("'menu' must be not null");
		this.menu = menu;
		
		MenuInflater inflater = activity.getMenuInflater();
    	inflater.inflate(R.menu.menu, menu); 	
    	refresh();
    	
    	switch(menuItemToDisable)
    	{
    		case R.id.itemAddTask:
    			menu.getItem(0).setEnabled(false);
    			break;
    		case R.id.itemEditCategories:
    			menu.getItem(1).setEnabled(false);
    			break;
    		case R.id.itemOldTasks:
    			menu.getItem(2).setEnabled(false);
    			break;
    		case R.id.itemSettings:
    			menu.getItem(3).setEnabled(false);
    			break;
	    	case R.id.itemAbout:
	    		menu.getItem(4).setEnabled(false);
	    		break;
    	}
    	
    	return true;
	}
	
	public void refresh()
	{
		if (menu != null)
		{
			String menuItemText = activity.getResources().getString(R.string.tasklist_activity_old);
			MenuItem itemOldTasks = menu.findItem(R.id.itemOldTasks);
	    	itemOldTasks.setTitle(menuItemText.replace("#0#", String.valueOf(dataManager.getOldPendingTaskCount())));
		}
	}
	
	public boolean onOptionsItemSelected(MenuItem item)
    {
		if (item == null) throw new IllegalArgumentException("'item' must be not null");
		boolean result = false;
    	
    	switch(item.getItemId())
    	{
    		case R.id.itemAddTask:
    			Intent addTaskIntent = new Intent(activity, EditTaskActivity.class);
    			String action = activity.getIntent().getStringExtra("action");
    			
    			if (action != null && !action.equals(""))
    			{
    				addTaskIntent.putExtra("view", action);  	
    				
    				if (action.equals("by-category"))
    				{
    					Serializable taskCategoryReceived = activity.getIntent().getSerializableExtra("category");
    					if (taskCategoryReceived != null && taskCategoryReceived instanceof TaskCategory)
    		            {
    		            	TaskCategory taskCategory = (TaskCategory)taskCategoryReceived;
    		            	addTaskIntent.putExtra("category", taskCategory);
    		            }
    				}
    			}

    			activity.startActivity(addTaskIntent);
    			break;
    		case R.id.itemEditCategories:
    			activity.startActivity(new Intent(activity, ManageTaskCategoriesActivity.class));
    			break;
    		case R.id.itemOldTasks:
    			Intent taskListIntent = new Intent(activity, TaskListActivity.class);
				taskListIntent.putExtra("action", "old");
				activity.startActivity(taskListIntent);
    			break;
    		case R.id.itemSettings:
    			activity.startActivity(new Intent(activity, SettingsActivity.class));
    			break;
	    	case R.id.itemAbout:
	    		activity.startActivity(new Intent(activity, AboutActivity.class));
	    		result = true;
	    		break;
    	}
    	
    	return result;
    }
}
